package com.qqq.adminservice.user.controller;

import com.qqq.adminservice.user.domain.dto.PasswordLoginDTO;
import com.qqq.adminservice.user.domain.dto.SysUserDTO;
import com.qqq.adminservice.user.domain.dto.SysUserListReqDTO;
import com.qqq.adminservice.user.domain.vo.SysUserLoginVO;
import com.qqq.adminservice.user.domain.vo.SysUserVO;
import com.qqq.adminservice.user.service.ISysUserService;
import com.qqq.commondomain.domain.R;
import com.qqq.commondomain.domain.dto.TokenDTO;
import com.qqq.commondomain.domain.vo.TokenVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * B端用户服务控制器类
 */
@RestController
@RequestMapping("/sys_user")
public class SysUserController {

    @Autowired
    private ISysUserService sysUserService;

    /**
     * ⼿机号、密码登录
     */
    @PostMapping("/login/password")
    public R<TokenVO> login(@Validated @RequestBody PasswordLoginDTO loginDTO)
    {
        // ⽤户登录，获取登录token
        TokenDTO tokenDTO = sysUserService.login(loginDTO);
        return R.ok(tokenDTO.convertToVO());
    }

    /**
     * 新增或编辑用户
     * @param sysUserDTO B端用户DTO
     * @return 用户Id
     */
    @PostMapping("/add_edit")
    public R<Long> addOrEditUser(@RequestBody SysUserDTO sysUserDTO) {
        return R.ok(sysUserService.addOrEditUser(sysUserDTO));
    }

    /**
     * 查询⽤户列表
     * @param sysUserListReqDTO
     * @return
     */
    @PostMapping("/list")
    public R<List<SysUserVO>> getUserList(@RequestBody SysUserListReqDTO sysUserListReqDTO) {
        List<SysUserDTO> sysUserDTOS =
                sysUserService.getUserList(sysUserListReqDTO);
        return R.ok(sysUserDTOS.stream()
                .map(SysUserDTO::convertToVO)
                .collect(Collectors.toList()));
    }

    /**
     * 获取B端用户登录信息
     * @return B端用户信息
     */
    @GetMapping("/login_info/get")
    public R<SysUserLoginVO> getLoginUser() {
        return R.ok(sysUserService.getLoginUser().convertToVO());
    }
}
